<template>
  <div class="alerts-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">求救信息列表</span>
        </div>
      </template>
      
      <div v-loading="loading">
        <div v-if="alertMessages.length === 0" class="empty-data">
          暂无求救信息
        </div>
        
        <el-card v-for="item in alertMessages" :key="item.id" class="alert-item" shadow="hover">
          <div class="alert-header">
            <div class="alert-time">
              <el-icon><Clock /></el-icon>
              {{ item.createTime }}
            </div>
            <el-tag :type="item.isRead ? 'info' : 'danger'" size="small">
              {{ item.isRead ? '已读' : '未读' }}
            </el-tag>
          </div>
          
          <div class="alert-content" v-if="item.parsedMsg">
            <!-- 求救用户信息 -->
            <div class="user-info">
              <div class="avatar">
                <el-avatar :src="item.parsedMsg.sosUser?.avatarUrl" :size="50"></el-avatar>
              </div>
              <div class="user-details">
                <div class="user-name">{{ item.parsedMsg.sosUser?.nickname }}</div>
                <div class="user-phone">
                  <el-icon><Phone /></el-icon>
                  {{ item.parsedMsg.sosUser?.phone }}
                </div>
              </div>
            </div>
            
            <!-- 行程信息 -->
            <div class="trip-info">
              <div class="info-row">
                <div class="info-label">行程状态</div>
                <div class="info-value">
                  <el-tag :type="getTripStatusType(item.parsedMsg.tripInfoVo?.tripStatus)">
                    {{ getTripStatusText(item.parsedMsg.tripInfoVo?.tripStatus) }}
                  </el-tag>
                </div>
              </div>
              <div class="info-row">
                <div class="info-label">预计出发</div>
                <div class="info-value">{{ formatDateTime(item.parsedMsg.tripInfoVo?.estimatedStartTime) }}</div>
              </div>
              <div class="info-row">
                <div class="info-label">已行驶</div>
                <div class="info-value">{{ item.parsedMsg.tripInfoVo?.traveledDistance }} 公里</div>
              </div>
            </div>
            
            <div class="actions">
              <el-button type="primary" size="small" @click="handleAlertAction(item)">
                处理求救
              </el-button>
              <el-button plain size="small" @click="markAsRead(item)" v-if="!item.isRead">
                标记为已读
              </el-button>
            </div>
          </div>
        </el-card>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Clock, Phone } from '@element-plus/icons-vue'
import { getAlertMessages, markAlertAsRead } from '@/api/alert'

// 数据定义
const loading = ref(false)
const alertMessages = ref([])

// 获取求救信息列表
const fetchAlertMessages = async () => {
  loading.value = true
  try {
    const res = await getAlertMessages()
    if (res.code === 1 && res.data) {
      // 解析消息中的JSON字符串
      alertMessages.value = res.data.map(item => {
        let parsedMsg = null
        try {
          parsedMsg = JSON.parse(item.msg)
        } catch (err) {
          console.error('解析消息内容失败:', err)
        }
        
        return {
          ...item,
          parsedMsg
        }
      })
    } else {
      ElMessage.error(res.msg || '获取求救信息失败')
    }
  } catch (error) {
    console.error('获取求救信息失败:', error)
    ElMessage.error('获取求救信息列表失败')
  } finally {
    loading.value = false
  }
}

// 格式化日期时间
const formatDateTime = (dateTimeStr) => {
  if (!dateTimeStr) return ''
  return dateTimeStr.replace('T', ' ').split('.')[0]
}

// 获取行程状态文本
const getTripStatusText = (status) => {
  const statusMap = {
    0: '未开始',
    1: '进行中',
    2: '已完成',
    3: '已取消'
  }
  return statusMap[status] || '未知状态'
}

// 获取行程状态标签类型
const getTripStatusType = (status) => {
  const typeMap = {
    0: 'info',
    1: 'warning',
    2: 'success',
    3: 'danger'
  }
  return typeMap[status] || 'info'
}

// 处理求救
const handleAlertAction = (item) => {
  // 实现求救处理逻辑
  console.log('处理求救:', item)
  ElMessage.success('求救处理功能待开发')
}

// 标记为已读
const markAsRead = async (item) => {
  try {
    const res = await markAlertAsRead(item.id)
    if (res.code === 1) {
      ElMessage.success('已标记为已读')
      item.isRead = true
    } else {
      ElMessage.error(res.msg || '操作失败')
    }
  } catch (error) {
    console.error('标记已读失败:', error)
    ElMessage.error('标记已读失败')
  }
}

// 页面加载时获取求救信息
onMounted(() => {
  fetchAlertMessages()
})
</script>

<style scoped>
.alerts-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 16px;
  font-weight: bold;
}

.empty-data {
  text-align: center;
  padding: 40px 0;
  color: #909399;
}

.alert-item {
  margin-bottom: 20px;
}

.alert-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}

.alert-time {
  display: flex;
  align-items: center;
  color: #909399;
}

.alert-time .el-icon {
  margin-right: 5px;
}

.alert-content {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  margin-right: 15px;
}

.user-details .user-name {
  font-weight: bold;
  margin-bottom: 5px;
}

.user-phone {
  display: flex;
  align-items: center;
  color: #606266;
}

.user-phone .el-icon {
  margin-right: 5px;
}

.trip-info {
  background-color: #f5f7fa;
  padding: 10px 15px;
  border-radius: 4px;
}

.info-row {
  display: flex;
  margin-bottom: 8px;
}

.info-row:last-child {
  margin-bottom: 0;
}

.info-label {
  width: 80px;
  color: #909399;
}

.actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 10px;
}

:deep(.el-card__header) {
  padding: 15px 20px;
}
</style> 